Completed
Push — master ( 6fa66c...ac2e46 )
by Dominik
08:33
created

bundler.plugin(ꞌdoneꞌ)   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
/*
2
 |--------------------------------------------------------------------------
3
 | Browser-sync config file
4
 |--------------------------------------------------------------------------
5
 |
6
 | For up-to-date information about the options:
7
 |   http://www.browsersync.io/docs/options/
8
 |
9
 | There are more options than you see here, these are just the ones that are
10
 | set internally. See the website for more info.
11
 |
12
 |
13
 */
14
const config = require('./build-config')
15
16
const webpack = require('webpack')
17
const webpackDevMiddleware = require('webpack-dev-middleware')
18
19
/**
20
 * Require ./webpack.config.js and make a bundler from it
21
 */
22
const webpackConfig = require('./webpack.config')
23
const bundler = webpack(webpackConfig)
24
25
const browserSync = require('browser-sync').create()
26
27
const crypto = require('crypto')
28
const fileHashes = {}
29
bundler.plugin('done', function (stats) {
30
  const changedFiles = Object.keys(stats.compilation.assets)
31
    .filter(name => {
32
      const asset = stats.compilation.assets[name]
33
      const md5Hash = crypto.createHash('md5')
34
      const hash = md5Hash.update(asset.children ? asset.children[0]._value : asset.source()).digest('hex')
35
      if (fileHashes[name] !== hash) {
36
        fileHashes[name] = hash
37
        return true
38
      } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
39
        return false
40
      }
41
    })
42
  browserSync.reload(changedFiles.map(name => `dist/${name}`))
43
})
44
45
browserSync.init(Object.assign({
46
  middleware: [
47
    webpackDevMiddleware(bundler, Object.assign({
48
      publicPath: webpackConfig[0].output.publicPath,
49
      logLevel: 'silent'
50
    }, config.webpackDevMiddleware))
51
  ]
52
}, config.browserSync))
53